package com.leyantech.ecp.agenx.task.filter.takeaction;

import com.leyantech.ai.DialogueReply;
import com.leyantech.ai.DialogueReplyV2;
import com.leyantech.ai.JobQueueName;
import com.leyantech.ai.ServiceMode;
import com.leyantech.ecp.agenx.common.service.StoreConfigService;
import com.leyantech.ecp.agenx.common.utils.PbPrintHelper;
import com.leyantech.ecp.agenx.task.filter.TaskFilter;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import lombok.extern.slf4j.Slf4j;

/**
 * @author eahau, {@literal <eahau@foxmail.com>}
 * @date 2022-11-15.
 */
@Slf4j
@Singleton
public class TakeActionFilter implements TaskFilter<DialogueReply> {

  private final StoreConfigService storeConfigService;

  @Inject
  public TakeActionFilter(StoreConfigService storeConfigService) {
    this.storeConfigService = storeConfigService;
  }

  @Override
  public boolean test(DialogueReply dialogueReply) {

    DialogueReplyV2 reply = dialogueReply.getDialogueReplyV2();
    JobQueueName jobQueueName = reply.getJobQueueName();

    if (jobQueueName == JobQueueName.QUESTION_ANSWER) {
      // 忽略问答消息
      return false;
    }

    final String storeId = dialogueReply.getStoreId();
    final String assistantId = dialogueReply.getAssistantId();

    if (JobQueueName.LXK == jobQueueName) {
      // lxk 消息，需要用lxk 坐席限制
      if (storeConfigService.isLxkAssistantForbidden(storeId, assistantId)) {
        log.warn("lxk坐席被限制，强制转人工.{}", PbPrintHelper.lazyToJson(reply));
        return false;
      }
    }

    // 如果不是辅助模式，则不需要考虑坐席限制
    if (!isNormalServiceMode(dialogueReply)) {
      log.info("storeId: {} 处于 {} 模式，不对坐席进行过滤", storeId, dialogueReply.getServiceMode());
      return false;
    }

    return true;
  }

  /**
   * 回复时，校验服务模式，只有正常接待模式（辅助），才会校验坐席.
   */
  public boolean isNormalServiceMode(DialogueReply dialogueReply) {
    ServiceMode serviceMode = dialogueReply.getServiceMode();
    if (serviceMode != ServiceMode.NORMAL) {
      log.info("门店:{} 处于{} 模式，不再对坐席进行过滤.", dialogueReply.getStoreId(), serviceMode);
      return false;
    }
    return true;
  }

}
